package com.ljtg.ljtgservice.controller.admin;

import com.ljtg.constant.JwtClaimsConstant;
import com.ljtg.ljtgpojo.dto.EmployeeLoginDTO;
import com.ljtg.ljtgpojo.entity.Employee;
import com.ljtg.ljtgpojo.vo.EmployeeLoginVO;
import com.ljtg.ljtgservice.service.EmployeeService;
import com.ljtg.properties.JwtProperties;
import com.ljtg.result.Result;
import com.ljtg.utils.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Tag(name = "员工管理接口", description = "员工登录接口")
public class EmployeeController {

        @Autowired
        private EmployeeService employeeService;
        @Autowired
        private JwtProperties jwtProperties;

        /**
         * 登录功能
         * @param employeeLoginDTO
         * @return
         */
        @PostMapping("/login")
        @Operation(summary = "员工登录", description = "员工登录并生成JWT令牌")
        public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
                log.info("员工登录:{}", employeeLoginDTO);

                Employee employee = employeeService.login(employeeLoginDTO);

                // 登录成功生成JWT令牌
                Map<String, Object> claims = new HashMap<>();
                claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
                String token = JwtUtil.createJWT(
                        jwtProperties.getAdminSecretKey(),
                        jwtProperties.getAdminTtl(),
                        claims);

                EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                        .id(employee.getId())
                        .userName(employee.getUsername())
                        .name(employee.getName())
                        .token(token)
                        .build();

                return Result.success(employeeLoginVO);
        }
}